/**
 * Created by Mak on 2016/4/22 0022.
 */
mui.init();

//主列表点击事件
mui('#list').on('tap', 'a', function() {
    var id = this.getAttribute('href');
    var href = this.href;
    var type = this.getAttribute("open-type");
    //不使用父子模板方案的页面
    if (type == "common") {
        var webview_style = {
            popGesture: "close"
        };
        //侧滑菜单需动态控制一下zindex值；
        if (~id.indexOf('offcanvas-')) {
            webview_style.zindex = 9998;
            webview_style.popGesture = ~id.indexOf('offcanvas-with-right') ? "close" : "none";
        }
        //图标界面需要启动硬件加速
        if(~id.indexOf('icons.html')){
            webview_style.hardwareAccelerated = true;
        }
        mui.openWindow({
            id: id,
            url: this.href,
            styles: webview_style,
            show: {
                aniShow: aniShow
            },
            waiting: {
                autoShow: false
            }
        });
    } else if (id && ~id.indexOf('.html')) {
        if (!mui.os.plus || (!~id.indexOf('popovers.html')&&mui.os.ios)) {
            mui.openWindow({
                id: id,
                url: this.href,
                styles: {
                    popGesture: 'close'
                },
                show: {
                    aniShow: aniShow
                },
                waiting: {
                    autoShow: false
                }
            });
        } else {
            //TODO  by chb 当初这么设计，是为了一个App中有多个模板，目前仅有一个模板的情况下，实际上无需这么复杂
            //使用父子模板方案打开的页面
            //获得共用模板组
            var template = getTemplate('default');
            //判断是否显示右上角menu图标；
            var showMenu = ~href.indexOf('popovers.html') ? true : false;
            //获得共用父模板
            var headerWebview = template.header;
            //获得共用子webview
            var contentWebview = template.content;
            var title = this.innerText.trim();
            //通知模板修改标题，并显示隐藏右上角图标；
            mui.fire(headerWebview, 'updateHeader', {
                title: title,
                showMenu: showMenu,
                target:href,
                aniShow: aniShow
            });

            if(mui.os.ios||(mui.os.android&&parseFloat(mui.os.version)<4.4)){
                var reload = true;
                if (!template.loaded) {
                    if (contentWebview.getURL() != this.href) {
                        contentWebview.loadURL(this.href);
                    } else {
                        reload = false;
                    }
                } else {
                    reload = false;
                }
                (!reload) && contentWebview.show();

                headerWebview.show(aniShow, 150);
            }
        }
    }
});


//侧滑容器父节点
mui('.mui-scroll-wrapper').scroll().scrollTo(0, 0, 100);
var offCanvasWrapper = mui('#offCanvasWrapper');
//主界面容器
var offCanvasInner = offCanvasWrapper[0].querySelector('.mui-inner-wrap');
//菜单容器
var offCanvasSide = document.getElementById("offCanvasSide");
if (!mui.os.android) {
    document.getElementById("move-togger").classList.remove('mui-hidden');
    var spans = document.querySelectorAll('.android-only');
    for (var i = 0, len = spans.length; i < len; i++) {
        spans[i].style.display = "none";
    }
}
//移动效果是否为整体移动
var moveTogether = false;
//侧滑容器的class列表，增加.mui-slide-in即可实现菜单移动、主界面不动的效果；
var classList = offCanvasWrapper[0].classList;
//变换侧滑动画移动效果；
mui('.mui-input-group').on('change', 'input', function () {
    if (this.checked) {
        offCanvasSide.classList.remove('mui-transitioning');
        offCanvasSide.setAttribute('style', '');
        classList.remove('mui-slide-in');
        classList.remove('mui-scalable');
        switch (this.value) {
            case 'main-move':
                if (moveTogether) {
                    //仅主内容滑动时，侧滑菜单在off-canvas-wrap内，和主界面并列
                    offCanvasWrapper[0].insertBefore(offCanvasSide, offCanvasWrapper[0].firstElementChild);
                }
                break;
            case 'main-move-scalable':
                if (moveTogether) {
                    //仅主内容滑动时，侧滑菜单在off-canvas-wrap内，和主界面并列
                    offCanvasWrapper[0].insertBefore(offCanvasSide, offCanvasWrapper[0].firstElementChild);
                }
                classList.add('mui-scalable');
                break;
            case 'menu-move':
                classList.add('mui-slide-in');
                break;
            case 'all-move':
                moveTogether = true;
                //整体滑动时，侧滑菜单在inner-wrap内
                offCanvasInner.insertBefore(offCanvasSide, offCanvasInner.firstElementChild);
                break;
        }
        offCanvasWrapper.offCanvas().refresh();
    }
});

//实现ios平台原生侧滑关闭页面；
if (mui.os.plus && mui.os.ios) {
    mui.plusReady(function () { //5+ iOS暂时无法屏蔽popGesture时传递touch事件，故该demo直接屏蔽popGesture功能
        plus.webview.currentWebview().setStyle({
            'popGesture': 'none'
        });
    });
}